//
//  LoginViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "LogonViewController.h"
#import "DataBaseSingleton.h"
#import "User.h"
#import "MBProgressHUD.h"

@interface LoginViewController ()<LoginViewDelegate, MBProgressHUDDelegate>

@property (nonatomic, retain)LoginView *loginV;
@property (nonatomic, retain)User *user;
@property (nonatomic, retain)MBProgressHUD *hud;

@end

@implementation LoginViewController

- (void)dealloc
{
    [_hud release];
    [_loginV release];
    [_user release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"用户登录";
    self.view.backgroundColor = [UIColor whiteColor];
// 左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
    self.loginV = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.loginV];
    [_loginV release];
#pragma mark -- 设置代理人
    self.loginV.loginDelegate = self;
}


// 左按钮
- (void)leftAction:(UIBarButtonItem *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
#pragma mark -- 实现协议方法
-(void)presentToViewController{
    LogonViewController *logonVC = [[LogonViewController alloc] init];
    [self.navigationController pushViewController:logonVC animated:YES];
    [logonVC release];
}
#pragma mark -- 实现协议方法
- (void)presentToViewControllerName:(NSString *)name Password:(NSString *)password{
    NSMutableArray *array = [DATABASE selectUserName:name];
    self.user = array.lastObject;
    if (array.count == 1 && self.user.password == password) {
        [NSUSER setObject:@"1" forKey:@"isLogin"];
        [NSUSER setObject:name forKey:@"name"];
        
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.labelText = @"登陆成功";
        self.hud.mode = MBProgressHUDModeText;
        self.hud.delegate = self;
        [self.hud hide:YES afterDelay:0.5];
        
        NSLog(@"登陆成功");
    } else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        // 创建提示框按钮
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
        }];
        // 将按钮放在提示框上
        [alertC addAction:action];
    }
}
#pragma mark -- hud消失时触发
-(void)hudWasHidden:(MBProgressHUD *)hud{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
